use "FILEPATH\ANES Cumulative.dta" 

*Demographics
gen age=VCF0101
replace age=. if VCF0101==0

gen agesq=age^2

gen female=1 if VCF0104==2
replace female=0 if VCF0104==1

gen weight=VCF0011z

gen education=VCF0140a
replace education=. if education>7

gen churchatt=VCF0130
replace churchatt=. if churchatt>=8
replace churchatt=5 if churchatt==7|churchatt==0
replace churchatt=churchatt-1

gen year=VCF0004

gen white=1 if VCF0105a==1
replace white=0 if VCF0105a!=1

gen black=1 if VCF0105a==2
replace black=0 if VCF0105a!=2

gen latino=1 if VCF0105a==5
replace latino=0 if VCF0105a!=5

gen asian=1 if VCF0105a==3
replace asian=0 if VCF0105a!=3

gen income=VCF0114
replace income=. if VCF0114==0

*feeling thermometers
gen poorfeel=VCF0223
replace poorfeel=. if poorfeel>97

gen blackfeel=VCF0206
replace blackfeel=. if blackfeel>97

gen hispfeel=VCF0217
replace hispfeel=. if hispfeel>97

gen illegalfeel=VCF0233
replace illegalfeel=. if illegalfeel>97

gen welffeel=VCF0220
replace welffeel=. if welffeel>97

foreach var of varlist blackfeel poorfeel hispfeel welffeel illegalfeel{
replace `var'=100 if `var'==97
}

*welfare spending
gen welfspend=-1 if VCF0894==3
replace welfspend=0 if VCF0894==2
replace welfspend=1 if VCF0894==1

gen welfspend01=(welfspend+1)/2

*party and ideology
gen partyid=VCF0301
replace partyid=. if partyid<1
replace partyid=partyid-1

gen libcon=VCF0803
replace libcon=. if VCF0803==0|VCF0803==9
replace libcon=libcon-1

*imm level
gen level=-1 if VCF0879a==5
replace level=0 if VCF0879a==3
replace level=1 if VCF0879a==1

*imm factor score
factor level illegalfeel [aweight=weight], pcf
predict immindex

*aid to blacks
gen aidblack=(VCF0830-1)/6
replace aidblack=. if aidblack<0|aidblack>1
replace aidblack=1-aidblack

gen aidblack3=0 if aidblack<.5
replace aidblack3=.5 if aidblack==.5
replace aidblack3=1 if aidblack>.5 & aidblack<=1


*other feeling thermometers
gen feministsft=VCF0253
replace feministsft=. if feministsft<0|feministsft>97
replace feministsft=100 if feministsft==97

gen gayft=VCF0232
replace gayft=. if gayft<0|gayft>97
replace gayft=100 if gayft==97
*Muslims and Atheists merged in separately from 2008 and 2012 ANES files


gen union=1 if VCF0127==1
replace union=0 if VCF0127==2

gen married=1 if VCF0147==1
replace married=0 if VCF0147>=2 & VCF0147<=8


*racial resentment
foreach var of varlist VCF9039 VCF9040 VCF9041 VCF9042{
gen `var'_1=(`var'-1)/4
replace `var'_1=. if `var'_1<0|`var'_1>1
}
replace VCF9039_1=1-VCF9039_1
replace VCF9042_1=1-VCF9042_1
egen racres=rmean(VCF9039_1 VCF9040_1 VCF9041_1 VCF9042_1)

egen boo=rmean(gayft feministsft ft_muslims ft_atheists)

*fair job for blacks
gen fairjob=1 if VCF9037==5
replace fairjob=2 if VCF9037==9
replace fairjob=3 if VCF9037==1

***************************************
*PLACEBO TEST 1
*Full Sample, Table 1 (abbreviated results) and Online Appendix, Table A1 (full results)
ologit welfspend01 illegalfeel blackfeel poorfeel partyid libcon female age agesq education income churchatt black latino asian i.year i.VCF0901a if welfspend01!=. & aidblack!=. [aweight=weight]
ologit aidblack3 illegalfeel blackfeel poorfeel partyid libcon female age agesq education income churchatt black latino asian i.year i.VCF0901a if welfspend01!=. & aidblack!=. [aweight=weight]
ologit welfspend01 level blackfeel poorfeel partyid libcon female age agesq education income churchatt black latino asian i.year i.VCF0901a if welfspend01!=. & aidblack!=. [aweight=weight]
ologit aidblack3 level blackfeel poorfeel partyid libcon female age agesq education income churchatt black latino asian i.year i.VCF0901a if welfspend01!=. & aidblack!=. [aweight=weight]
ologit welfspend01 immindex blackfeel poorfeel partyid libcon female age agesq education income churchatt black latino asian i.year i.VCF0901a if welfspend01!=. & aidblack!=. [aweight=weight]
ologit aidblack3 immindex blackfeel poorfeel partyid libcon female age agesq education income churchatt black latino asian i.year i.VCF0901a if welfspend01!=. & aidblack!=. [aweight=weight]


*Non-hispanic whites (Online Appendix, Table A2)
ologit welfspend01 illegalfeel blackfeel poorfeel partyid libcon female age agesq education income churchatt i.year i.VCF0901a if welfspend01!=. & aidblack!=. & white==1 [aweight=weight]
ologit aidblack3 illegalfeel blackfeel poorfeel partyid libcon female age agesq education income churchatt i.year i.VCF0901a if welfspend01!=. & aidblack!=. & white==1 [aweight=weight]
ologit welfspend01 level blackfeel poorfeel partyid libcon female age agesq education income churchatt i.year i.VCF0901a if welfspend01!=. & aidblack!=. & white==1 [aweight=weight]
ologit aidblack3 level blackfeel poorfeel partyid libcon female age agesq education income churchatt i.year i.VCF0901a if welfspend01!=. & aidblack!=. & white==1 [aweight=weight]
ologit welfspend01 immindex blackfeel poorfeel partyid libcon female age agesq education income churchatt i.year i.VCF0901a if welfspend01!=. & aidblack!=. & white==1 [aweight=weight]
ologit aidblack3 immindex blackfeel poorfeel partyid libcon female age agesq education income churchatt i.year i.VCF0901a if welfspend01!=. & aidblack!=. & white==1 [aweight=weight]


*Hussey & Pearson-Merkowitz (2012) Specification
mlogit welfspend01 illegalfeel education income age female married union libcon govlimit blackslazy black latino i.year if year==2004|year==2008, baseoutcome(0)
mlogit aidblack3 illegalfeel education income age female married union libcon govlimit blackslazy black latino i.year if year==2004|year==2008, baseoutcome(0)

***********************************************************************************************************
*PLACEBO TEST 2

*Exploratory Factor Analysis of Outgroup Feeling Thermometers (Online Appendix, Table A4)
factor welffeel illegalfeel ft_atheists ft_muslims gayft feministsft [aweight=weight], mineigen(1)

*full sample (Table 2, abbreviated results and Online Appendix, Table A5, full results)
reg c.welffeel illegalfeel blackfeel poorfeel partyid libcon female age agesq education income churchatt black latino asian i.year i.VCF0901a [aweight=weight] if year==2008|year==2012
reg c.welffeel ft_muslims blackfeel poorfeel partyid libcon female age agesq education income churchatt black latino asian i.year i.VCF0901a [aweight=weight] if year==2008|year==2012
reg c.welffeel ft_atheists blackfeel poorfeel partyid libcon female age agesq education income churchatt black latino asian i.year i.VCF0901a [aweight=weight] if year==2008|year==2012
reg c.welffeel feministsft blackfeel poorfeel partyid libcon female age agesq education income churchatt black latino asian i.year i.VCF0901a [aweight=weight] if year==2008|year==2012
reg c.welffeel gayft blackfeel poorfeel partyid libcon female age agesq education income churchatt black latino asian i.year i.VCF0901a [aweight=weight] if year==2008|year==2012


ologit c.welfspend01 illegalfeel blackfeel poorfeel partyid libcon female age agesq education income churchatt black latino asian i.year i.VCF0901a [aweight=weight] if year==2008|year==2012
ologit c.welfspend01 ft_muslims blackfeel poorfeel partyid libcon female age agesq education income churchatt black latino asian i.year i.VCF0901a [aweight=weight] if year==2008|year==2012
ologit c.welfspend01 ft_atheists blackfeel poorfeel partyid libcon female age agesq education income churchatt black latino asian i.year i.VCF0901a [aweight=weight] if year==2008|year==2012
ologit c.welfspend01 feministsft blackfeel poorfeel partyid libcon female age agesq education income churchatt black latino asian i.year i.VCF0901a [aweight=weight] if year==2008|year==2012
ologit c.welfspend01 gayft blackfeel poorfeel partyid libcon female age agesq education income churchatt black latino asian i.year i.VCF0901a [aweight=weight] if year==2008|year==2012

*non-Hispanic whites (Online Appendix, Table A6)
reg c.welffeel illegalfeel blackfeel poorfeel partyid libcon female age agesq education income churchatt i.year i.VCF0901a [aweight=weight] if (year==2008|year==2012) & white==1
reg c.welffeel ft_muslims blackfeel poorfeel partyid libcon female age agesq education income churchatt i.year i.VCF0901a [aweight=weight] if (year==2008|year==2012) & white==1
reg c.welffeel ft_atheists blackfeel poorfeel partyid libcon female age agesq education income churchatt i.year i.VCF0901a [aweight=weight] if (year==2008|year==2012) & white==1
reg c.welffeel feministsft blackfeel poorfeel partyid libcon female age agesq education income churchatt i.year i.VCF0901a [aweight=weight] if (year==2008|year==2012) & white==1
reg c.welffeel gayft blackfeel poorfeel partyid libcon female age agesq education income churchatt i.year i.VCF0901a [aweight=weight] if (year==2008|year==2012) & white==1

ologit c.welfspend01 illegalfeel blackfeel poorfeel partyid libcon female age agesq education income churchatt i.year i.VCF0901a [aweight=weight] if (year==2008|year==2012) & white==1
ologit c.welfspend01 ft_muslims blackfeel poorfeel partyid libcon female age agesq education income churchatt i.year i.VCF0901a [aweight=weight] if (year==2008|year==2012) & white==1
ologit c.welfspend01 ft_atheists blackfeel poorfeel partyid libcon female age agesq education income churchatt i.year i.VCF0901a [aweight=weight] if (year==2008|year==2012) & white==1
ologit c.welfspend01 feministsft blackfeel poorfeel partyid libcon female age agesq education income churchatt i.year i.VCF0901a [aweight=weight] if (year==2008|year==2012) & white==1
ologit c.welfspend01 gayft blackfeel poorfeel partyid libcon female age agesq education income churchatt i.year i.VCF0901a [aweight=weight] if (year==2008|year==2012) & white==1

*H&P-M 
mlogit welfspend01 illegalfeel education income age female married union libcon govlimit blackslazy black latino i.year if year==2004|year==2008, baseoutcome(0)
mlogit welfspend01 ft_muslims education income age female married union libcon govlimit blackslazy black latino i.year if year==2004|year==2008, baseoutcome(0)
mlogit welfspend01 feministsft education income age female married union libcon govlimit blackslazy black latino i.year if year==2004|year==2008, baseoutcome(0)
mlogit welfspend01 ft_atheists education income age female married union libcon govlimit blackslazy black latino i.year if year==2004|year==2008, baseoutcome(0)
mlogit welfspend01 gayft education income age female married union libcon govlimit blackslazy black latino i.year if year==2004|year==2008, baseoutcome(0)


************************
*Confounding and BOO
egen ftindex=rmean(gayft feministsft ft_muslims ft_atheists)
replace ftindex=. if gayft==.|feministsft==.|ft_muslims==.|ft_atheists==.

*full sample (Online Appendix, Table A8)
ologit welfspend01 immindex blackfeel poorfeel partyid libcon female age agesq education income churchatt black latino asian i.year i.VCF0901a [aweight=weight] if (year==2008|year==2012)
ologit welfspend01 immindex ftindex racres blackfeel poorfeel partyid libcon female age agesq education income churchatt black latino asian i.year i.VCF0901a [aweight=weight] if (year==2008|year==2012)
reg welffeel immindex blackfeel poorfeel partyid libcon female age agesq education income churchatt black latino asian i.year i.VCF0901a [aweight=weight] if (year==2008|year==2012)
reg welffeel immindex ftindex racres blackfeel poorfeel partyid libcon female age agesq education income churchatt black latino asian i.year i.VCF0901a [aweight=weight] if (year==2008|year==2012)

*non-Hispanic whites (Online Appendix, Table A9)
ologit welfspend01 immindex blackfeel poorfeel partyid libcon female age agesq education income churchatt i.year i.VCF0901a [aweight=weight] if (year==2008|year==2012) & white==1
ologit welfspend01 immindex ftindex racres blackfeel poorfeel partyid libcon female age agesq education income churchatt i.year i.VCF0901a [aweight=weight] if (year==2008|year==2012) & white==1
reg welffeel immindex blackfeel poorfeel partyid libcon female age agesq education income churchatt i.year i.VCF0901a [aweight=weight] if (year==2008|year==2012) & white==1
reg welffeel immindex ftindex racres blackfeel poorfeel partyid libcon female age agesq education income churchatt i.year i.VCF0901a [aweight=weight] if (year==2008|year==2012) & white==1

***************************

*CHANGE OVER TIME

*The following syntax can be used to re-create Figure 2 and Online Appendix, Figures A1 and A2.
*It requires installation of parmest package to use the parmby command and installation of the eclplot command to create each figure.  
*Insert filepath where you see FILEPATH.  
*Create each graph sequentially and re-open the master data set 
*after creating each figure to create the next.
*Use graphcombine("FILENAMES") to merge the graphs as displayed in the paper and Online Appendix.

set more off

*Full Sample (Figure 2)

parmby "reg welffeel illegalfeel blackfeel poorfeel partyid libcon female age agesq education income churchatt i.VCF0901a [pweight=weight]", by(year) saving("FILEPATH\illegalwelffeelovertime")
/*use "FILEPATH\illegalwelffeelovertime.dta", clear
keep if parm=="illegalfeel"
eclplot estimate min95 max95 year, addplot(lfit estimate year)
*/

parmby "reg welffeel level blackfeel poorfeel partyid libcon female age agesq education income churchatt i.VCF0901a [pweight=weight]", by(year) saving("FILEPATH\levelwelffeelovertime")
/* use "FILEPATH\levelwelffeelovertime.dta", clear
keep if parm=="level"
eclplot estimate min95 max95 year, addplot(lfit estimate year)
*/

parmby "reg welffeel immindex blackfeel poorfeel partyid libcon female age agesq education income churchatt i.VCF0901a [pweight=weight]", by(year) saving("FILEPATH\immindexwelffeelovertime")
/* use "FILEPATH\immindexwelffeelovertime.dta", clear
keep if parm=="immindex"
eclplot estimate min95 max95 year, addplot(lfit estimate year)
*/

parmby "ologit welfspend01 illegalfeel blackfeel poorfeel partyid libcon female age agesq education income churchatt i.VCF0901a [pweight=weight]", by(year) saving("FILEPATH\illegalwelfspendovertime")
/* use "FILEPATH\illegalwelfspendovertime.dta", clear
keep if parm=="illegalfeel"
eclplot estimate min95 max95 year, addplot(lfit estimate year)
*/

parmby "ologit welfspend01 level blackfeel poorfeel partyid libcon female age agesq education income churchatt i.VCF0901a [pweight=weight]", by(year) saving("FILEPATH\levelwelfspendovertime")
/* use "FILEPATH\levelwelfspendovertime.dta", clear
keep if parm=="level"
eclplot estimate min95 max95 year, addplot(lfit estimate year)
*/

parmby "ologit welfspend01 immindex blackfeel poorfeel partyid libcon female age agesq education income churchatt i.VCF0901a [pweight=weight]", by(year) saving("FILEPATH\immindexwelfspendovertime")
/* use "FILEPATH\immindexwelfspendovertime.dta", clear
keep if parm=="immindex"
eclplot estimate min95 max95 year, addplot(lfit estimate year)
*/


*non-hispanic whites

parmby "reg welffeel illegalfeel blackfeel poorfeel partyid libcon female age agesq education income churchatt i.VCF0901a [pweight=weight] if white==1", by(year) saving("FILEPATH\illegalwelffeelovertimeW")
/* use "FILEPATH\illegalwelffeelovertimeW.dta", clear
keep if parm=="illegalfeel"
eclplot estimate min95 max95 year, addplot(lfit estimate year)
*/

parmby "reg welffeel level blackfeel poorfeel partyid libcon female age agesq education income churchatt i.VCF0901a [pweight=weight] if white==1", by(year) saving("FILEPATH\levelwelffeelovertimeW")
/* use "FILEPATH\levelwelffeelovertimeW.dta", clear
keep if parm=="level"
eclplot estimate min95 max95 year, addplot(lfit estimate year)
*/

parmby "reg welffeel immindex blackfeel poorfeel partyid libcon female age agesq education income churchatt i.VCF0901a [pweight=weight] if white==1", by(year) saving("FILEPATH\immindexwelffeelovertimeW")
/* use "FILEPATH\immindexwelffeelovertimeW.dta", clear
keep if parm=="immindex"
eclplot estimate min95 max95 year, addplot(lfit estimate year)
*/

parmby "ologit welfspend01 illegalfeel blackfeel poorfeel partyid libcon female age agesq education income churchatt i.VCF0901a [pweight=weight] if white==1", by(year) saving("FILEPATH\illegalwelfspendovertimeW")
/* use "FILEPATH\illegalwelfspendovertimeW.dta", clear
keep if parm=="illegalfeel"
eclplot estimate min95 max95 year, addplot(lfit estimate year)
*/

parmby "ologit welfspend01 level blackfeel poorfeel partyid libcon female age agesq education income churchatt i.VCF0901a [pweight=weight] if white==1", by(year) saving("FILEPATH\levelwelfspendovertimeW")
/* use "FILEPATH\levelwelfspendovertimeW.dta", clear
keep if parm=="level"
eclplot estimate min95 max95 year, addplot(lfit estimate year)
*/

parmby "ologit welfspend01 immindex blackfeel poorfeel partyid libcon female age agesq education income churchatt i.VCF0901a [pweight=weight] if white==1", by(year) saving("FILEPATH\immindexwelfspendovertimeW")
/* use "FILEPATH\immindexwelfspendovertimeW.dta", clear
keep if parm=="immindex"
eclplot estimate min95 max95 year, addplot(lfit estimate year)
*/


*hispanics FT and welffeel for longer time series (Online Appendix, Figure A2)

parmby "reg welffeel hispfeel blackfeel poorfeel partyid libcon female age agesq education income churchatt i.VCF0901a [pweight=weight]", by(year) saving("FILEPATH\hispovertime")
/* use "FILEPATH\hispovertime.dta", clear
keep if parm=="hispfeel"
eclplot estimate min95 max95 year, addplot(lfit estimate year)
*/


clear

***************************
*1992-96 panel analysis
***************************

use "FILEPATH\anes92-96panel.dta", clear
gen level92=-1 if V926235==4|V926235==5
replace level92=0 if V926235==3
replace level92=1 if V926235==1|V926235==2

gen illimmft92=V925335
replace illimmft92=. if illimmft92<0|illimmft92>100

gen level94=-1 if V941016==4|V941016==5
replace level94=0 if V941016==3
replace level94=1 if V941016==1|V941016==2

gen illimmft94=V940317
replace illimmft94=. if illimmft94<0|illimmft94>100

gen weightpanel94=V940005

factor illimmft92 level92 [aweight=weightpanel94]
predict immindex92

factor illimmft94 level94 [aweight=weightpanel94]
predict immindex94

gen welfspend92=1 if V923726==1
replace welfspend92=.5 if V923726==2
replace welfspend92=0 if V923726==3|V923726==7

gen welfspend94=1 if V940820==1
replace welfspend94=.5 if V940820==2
replace welfspend94=0 if V940820==3|V940820==7

gen welffeel92=V925318
replace welffeel92=. if welffeel92<0|welffeel92>100

gen welffeel94=V940309
replace welffeel94=. if welffeel94<0|welffeel94>100

gen latino=1 if V924121==1|V924122==1
replace latino=0 if latino!=1

gen white=1 if V924202==1 & latino!=1
replace white=0 if V924202!=1|latino==1

gen black=1 if V924202==2 & latino!=1
replace black=0 if V924202!=2|latino==1

gen asian=1 if V924202==4 & latino!=1
replace asian=0 if V924202!=4|latino==1

foreach var of varlist latino white black asian{
replace `var'=. if V924202==.
}

gen partyid92=V923634
replace partyid92=. if V923634>6

gen blackfeel=V925323
replace blackfeel=. if blackfeel<0|blackfeel>100
rename blackfeel blackfeel92 

gen female=1 if V924201==2
replace female=0 if V924201==1

gen income92=V924104
replace income92=. if income92>24

gen educ92=V923908
replace educ92=. if educ92<1|educ92>7

gen age92=V923903

gen age92sq=age92^2

gen libcon92=V923509
replace libcon92=. if libcon92<1|libcon92>7

gen attend=V923828
replace attend=0 if attend==5
replace attend=. if attend>5
replace attend=. if V923826==0
rename attend attend92

gen poorfeel=V925320
replace poorfeel=. if poorfeel>100|poorfeel<0
rename poorfeel poorfeel92

reg welffeel94 welffeel92 illimmft92 blackfeel92 poorfeel92 partyid92 libcon92 female age92 age92sq educ92 income92 attend92 white black latino asian i.V923018 [aweight=weightpanel94]
reg welffeel94 welffeel92 level92 blackfeel92 poorfeel92 partyid92 libcon92 female age92 age92sq educ92 income92 attend92 white black latino asian i.V923018 [aweight=weightpanel94]
reg welffeel94 welffeel92 immindex92 blackfeel92 poorfeel92 partyid92 libcon92 female age92 age92sq educ92 income92 attend92 white black latino asian i.V923018 [aweight=weightpanel94]
reg illimmft94 welffeel92 illimmft92 blackfeel92 poorfeel92 partyid92 libcon92 female age92 age92sq educ92 income92 attend92 white black latino asian i.V923018 [aweight=weightpanel94]
reg level94 welffeel92 level92 blackfeel92 poorfeel92 partyid92 libcon92 female age92 age92sq educ92 income92 attend92 white black latino asian i.V923018 [aweight=weightpanel94]
reg immindex94 welffeel92 immindex92 blackfeel92 poorfeel92 partyid92 libcon92 female age92 age92sq educ92 income92 attend92 white black latino asian i.V923018 [aweight=weightpanel94]

clear
